/*

  utf.h
  Shan.Lib

 Copyright (c) 2013, Sung-Han Park
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright notice,
 this list of conditions and the following disclaimer in the documentation
 and/or other materials provided with the distribution.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#ifndef _Shan_unicode_h_
#define _Shan_unicode_h_

#include <locale>
//#include <codecvt>

namespace shan {
	class utf {
	public:
		static bool is_lead_surrogate(char16_t utf16) {
			return ((utf16 & 0xFC00) == 0xD800);
		}

		static bool is_trail_surrogate(char16_t utf16) {
			return ((utf16 & 0xFC00) == 0xDC00);
		}

		static char32_t surrogate_pair_to_utf32(char16_t lead, char16_t trail) {
			return (((lead & 0x03FF) << 10) + (trail & 0x03FF)) + 0x00010000;
		}
		
		static std::string to_utf8(char16_t utfnn) {
			return to_utf8((char32_t)utfnn);
		}

		static std::string to_utf8(char32_t utfnn) {
			std::string ret;

			if (utfnn < 0x80) {
				ret.push_back(static_cast<char>(utfnn));
			}
			else if (utfnn < 0x800) {
				ret.push_back(static_cast<char>((utfnn >> 6) | 0xc0));
				ret.push_back(static_cast<char>((utfnn & 0x3f) | 0x80));
			}
			else if (utfnn < 0x10000) {
				ret.push_back(static_cast<char>((utfnn >> 12) | 0xe0));
				ret.push_back(static_cast<char>(((utfnn >> 6) & 0x3f) | 0x80));
				ret.push_back(static_cast<char>((utfnn & 0x3f) | 0x80));
			}
			else {
				ret.push_back(static_cast<char>((utfnn >> 18) | 0xf0));
				ret.push_back(static_cast<char>(((utfnn >> 12) & 0x3f) | 0x80));
				ret.push_back(static_cast<char>(((utfnn >> 6) & 0x3f) | 0x80));
				ret.push_back(static_cast<char>((utfnn & 0x3f) | 0x80));
			}

			return ret;
		}

		// g++ does not support std::codecvt_utf8, std::codecvt_utf8_utf16.
		// So below methods were commented. And above to_utf8() methods were defined.
/*
		template <class T>
		static std::string to_utf8(T utf_mb) {
			std::wstring_convert<std::codecvt_utf8<T>, T> conv;
			return conv.to_bytes(utf_mb);
		}

		template <class T>
		static std::string to_utf8(const T* utf_mb) {
			std::wstring_convert<std::codecvt_utf8<T>, T> conv;
			return conv.to_bytes(utf_mb);
		}

		template <class T>
		static std::string to_utf8(const std::basic_string<T>& utf_mb) {
			std::wstring_convert<std::codecvt_utf8<T>, T> conv;
			return conv.to_bytes(utf_mb);
		}

		template <class T>
		static std::string to_utf8(const T* first_utf_mb, const T* last_utf_mb) {
			std::wstring_convert<std::codecvt_utf8<T>, T> conv;
			return conv.to_bytes(first_utf_mb, last_utf_mb);
		}

		static std::u16string to_utf16(const std::string& utf8) {
			std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
			return conv.from_bytes(utf8);
		}

		static std::u32string to_utf32(const std::string& utf8) {
			std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
			return conv.from_bytes(utf8);
		}
*/
	};
}

#endif
